/*文章的详情页信息*/
window.onload = function() {
    $(function() {
        function changetime(tt) {
            let timeSpanstr = "";
            // 获取当前时间
            let date = new Date();
            let nowdate = date.getTime();
            //返回来的时间
            let atime = new Date(tt);
            let replytime = atime.getTime();
            let needtime = nowdate - replytime;

            if (needtime <= 1000 * 60 * 1) {
                timeSpanStr = "刚刚";
            } else if (1000 * 60 * 1 < needtime && needtime <= 1000 * 60 * 60) {
                timeSpanStr = Math.round(needtime / (1000 * 60)) + "分钟前";
            } else if (
                1000 * 60 * 60 * 1 < needtime &&
                needtime <= 1000 * 60 * 60 * 24
            ) {
                timeSpanStr = Math.round(needtime / (1000 * 60 * 60)) + "小时前";
            } else if (
                1000 * 60 * 60 * 24 < needtime &&
                needtime <= 1000 * 60 * 60 * 24 * 15
            ) {
                timeSpanStr = Math.round(needtime / (1000 * 60 * 60 * 24)) + "天前";
            }
            return timeSpanStr;
        }

        let url = location.pathname;
        let id = url.split("/")[2];
        console.log(id);

        $.ajax({
            type: "get",
            url: "https://cnodejs.org/api/v1/topic/" + id,
            async: false,
            dataType: "json",
            success: function(res) {
                console.log(res);
                let data = res.data;
                // let titlecell = [];
                // for (let k in data) {
                //     for (let i = 0; i < data[k].length; i++) {
                //         let ti = data[k][i]
                //         titlecell.push(ti)
                //     }
                // }

                let details_html = "";
                let slidebar_html = "";
                // for (let i = 0; i < titlecell.length; i++) {
                changetime(data.last_reply_at);
                slidebar_html += `
                        <p>作者: ${data.author.loginname}</p>
                        <img src="${data.author.avatar_url}" alt="">
                        <p>浏览量 ${data.visit_count}</p>
                        `;

                details_html += `
                        <div class="detailscontent">
                        <div class="detailtit">
                            <span class="totop">置顶</span>
                            <h3>${data.title}</h3>
                            <div class="littlespan">
                                <span>发布于${data.create_at}</span>
                                <span>作者${data.author.loginname}</span>
                                <span>来自${data.tab}</span>
                                <span>最后一次回复${timeSpanStr}</span>
                            </div>
                        </div>
                        <div class="detailscon">${data.content}</div>
                    </div>
                `;

                // }
                $("#titlecell").html(details_html);
                $("#authormess").html(slidebar_html);
            },
            error: function() {
                alert("error"); //错误的处理
            },
        });
    });
};